package com.gxa.wmcrm.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * 批量查询
 */
public class HibernateUtil {

    //  获取加载配置管理类读取配置信息
    public static Configuration getConfiguration() {
        // 获取加载配置管理类
        Configuration configuration = new Configuration();
        // 不给参数就默认加载hibernate.cfg.xml文件，
        configuration.configure();
        return configuration;
    }

    // 获取Session工厂对象
    public static SessionFactory getSessionFactory(Configuration configuration) {
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        return sessionFactory;
    }

    // 获取Session对象
    public static Session getSession(SessionFactory sessionFactory) {
        Session session = sessionFactory.openSession();
        return session;
    }


    // 获取Transaction对象
    public static Transaction getTransaction(Session session) {
        Transaction transaction = session.getTransaction();
        return transaction;
    }


    // 创建Session会话对象
    public static Session createSession(){
        Session session = getSession(
                getSessionFactory(
                        getConfiguration()
                )
        );
        return session;
    }

}


